package com.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.constant.MessageConstant;
import com.shop.entity.QueryPageBean;
import com.shop.entity.Result;
import com.shop.pojo.User;
import com.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Reference
    private UserService userService;

//    @PreAuthorize("hasAuthority('ADD_USER')")//权限校验
    @PostMapping("/add")
    @ApiOperation("添加用户接口")
    public Result add(@RequestBody User user){
        if (userService.findUserName(user.getUsername())!=null){
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }else{
            userService.add(user);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        }
    }
    @PreAuthorize("hasAuthority('QUERY_USER')")
    @RequestMapping("/pageQuery")
    @ApiOperation("用户分页接口")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Page<User> page=userService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return new Result(true,MessageConstant.QUERY_USER_SUCCESS,page);
    }

//    @PreAuthorize("hasAuthority('DELETE_USER')")
    @DeleteMapping("/delete")
    @ApiOperation("删除用户接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "int",name = "id",value = "用户id",required = true,example = "0")})
    public Result delete(Integer id){
        try {
            userService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }

//    @PreAuthorize("hasAuthority('EDIT_USER')")
    @PutMapping("/edit")
    @ApiOperation("修改用户接口")
    public Result edit(@RequestBody User user){
//        try {
//            userService.edit(user);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false,MessageConstant.EDIT_USER_FAIL);
//        }
//        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        User u = userService.findById(user.getId());
        if (userService.findUserName(user.getUsername())!=null && !u.getUsername().equals(user.getUsername())){
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }else{
            userService.edit(user);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        }
    }

    @GetMapping("/findById")
    @ApiOperation("根据用户id查找用户接口")
    public Result findById(@RequestParam(required = true) Integer id){
        try {
            User user=userService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("显示用户下拉列表接口")
    public Result findAll(){
        try {
            List<User> list=userService.findAll();
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

}
